home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / Duck Report / _SETUP.1 / DBBarcode.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-10-23  |  9.9 KB  |  348 lines

  1. Unit DBBarcode;
  2.  
  3. Interface
  4.  
  5. Uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ExtCtrls, DBarcode, DB, DBCtrls;
  8.  
  9. Type
  10.  
  11.     { ---------- TDBDuckBarCode ---------- }
  12.     TDBDuckBarCode = Class (TCustomControl)
  13.   Private
  14.   Protected
  15.       FBarcode:    TDuckBarcode;
  16.      FDataLink:    TFieldDataLink;
  17.         FPicture:    TPicture;
  18.      FBorderStyle:    TBorderStyle;
  19.  
  20.      Procedure    SetStyle (Const Value: TBarcodeStyle);
  21.      Function        GetStyle: TBarcodeStyle;
  22.      Procedure    SetRotation (Const Value: TBarcodeRotation);
  23.      Function        GetRotation: TBarcodeRotation;
  24.      Procedure    SetAddOn (Const Value: String);
  25.      Function        GetAddOn: String;
  26.      Procedure    SetHeightMM (Const Value: Double);
  27.      Function        GetHeightMM: Double;
  28.      Procedure    SetColor (Const Value: TColor);
  29.      Function        GetColor: TColor;
  30.      Procedure    SetColorBK (Const Value: TColor);
  31.      Function        GetColorBK: TColor;
  32.      Procedure    SetFont (Const Value: TFont);
  33.      Function        GetFont: TFont;
  34.      Procedure    SetDensity (Const Value: Integer);
  35.      Function        GetDensity: Integer;
  36.      Procedure    SetAlignment (Const Value: TAlignment);
  37.      Function        GetAlignment: TAlignment;
  38.      Procedure    SetBarWidth (Const Value: Double);
  39.      Function        GetBarWidth: Double;
  40.      Procedure    SetOptions (Const Value: TBarcodeOptions);
  41.      Function        GetOptions: TBarcodeOptions;
  42.  
  43.      Function        GetDataField: String;
  44.      Procedure    SetDataField (Const Value: String);
  45.      Function        GetDataSource: TDataSource;
  46.         Procedure    SetDataSource (Value: TDataSource);
  47.      Procedure    SetPicture (Value: TPicture);
  48.      Function        GetField: TField;
  49.      Procedure    SetBorderStyle(Value: TBorderStyle);
  50.      Procedure    DataChange(Sender: TObject);
  51.      Procedure    CreateParams (var Params: TCreateParams); Override;
  52.      Procedure    Loaded; Override;
  53.      Procedure    Paint; Override;
  54.   Public
  55.       Constructor    Create(owner: TComponent); Override;
  56.         Destructor    Destroy; Override;
  57.      Property        Barcode:    TDuckBarcode Read FBarcode;
  58.      Function        Print (rLeft, rTop: Real): TRect;
  59.      Function        PaintPoint (Canvas: TCanvas; pt: TPoint): TRect;
  60.      Procedure    PaintRect (Canvas: TCanvas; Rc: TRect);
  61.  
  62.      property        Picture: TPicture read FPicture write SetPicture;
  63.      property        Field: TField read GetField;
  64.   Published
  65.       Property Alignment:    TAlignment Read GetAlignment Write SetAlignment;
  66.         Property Style:        TBarcodeStyle Read GetStyle Write SetStyle;
  67.      Property Rotation:    TBarcodeRotation Read GetRotation Write SetRotation;
  68.      Property AddOn:        String Read GetAddOn Write SetAddOn;
  69.      Property HeightMM:    Double Read GetHeightMM Write SetHeightMM;
  70.      Property ColorBar:    TColor Read GetColor Write SetColor;
  71.      Property ColorBK:        TColor Read GetColorBK Write SetColorBK;
  72.      Property Font:            TFont Read GetFont Write SetFont;
  73.      Property Density:        Integer Read GetDensity Write SetDensity;
  74.      Property BarWidthMM:    Double Read GetBarWidth Write SetBarWidth;
  75.      Property Options:        TBarcodeOptions Read GetOptions Write SetOptions
  76.                                  Default [boShowText, boCheckCRC];
  77.  
  78.      Property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsSingle;
  79.      Property Color;
  80.      Property Ctl3D;
  81.      Property DataField: string read GetDataField write SetDataField;
  82.      Property DataSource: TDataSource read GetDataSource write SetDataSource;
  83.     End;
  84.  
  85. Procedure Register;
  86.  
  87. Implementation
  88.  
  89. Procedure Register;
  90. Begin
  91.     RegisterComponents('DuckTech', [TDBDuckBarCode]);
  92. End;
  93.  
  94. { ---------- TDBDuckBarCode ---------- }
  95. Constructor TDBDuckBarCode.Create(owner: TComponent);
  96. Begin
  97.     inherited Create(owner);
  98.     FBarcode    := TDuckBarcode.Create (Self);
  99.   
  100.   FPicture := TPicture.Create;
  101.   Color        := clWhite;
  102.   ControlStyle := ControlStyle + [csOpaque, csReplicatable];
  103.     if not NewStyleControls then
  104.       ControlStyle := ControlStyle + [csFramed];
  105.   FDataLink := TFieldDataLink.Create;
  106.   FDataLink.Control := Self;
  107.   FDataLink.OnDataChange := DataChange;
  108.   FBorderStyle := bsSingle;
  109. End;
  110. Destructor TDBDuckBarCode.Destroy;
  111. Begin
  112.     FPicture.Free;
  113.     FDataLink.Free;
  114.   FDataLink := nil;
  115.     FBarcode.Free;
  116.   inherited Destroy;
  117. End;
  118. Procedure TDBDuckBarCode.SetStyle (Const Value: TBarcodeStyle);
  119. Begin
  120.     FBarcode.Style    := Value;
  121.   Picture.Graphic    := FBarcode.GetBitmap;
  122.     Invalidate;
  123. End;
  124. Function TDBDuckBarCode.GetStyle: TBarcodeStyle;
  125. Begin
  126.     Result    := FBarcode.Style; 
  127. End;
  128. Procedure TDBDuckBarCode.SetRotation (Const Value: TBarcodeRotation);
  129. Begin
  130.     FBarcode.Rotation    := Value;
  131.   Picture.Graphic    := FBarcode.GetBitmap;
  132.     Invalidate;
  133. End;
  134. Function TDBDuckBarCode.GetRotation: TBarcodeRotation;
  135. Begin
  136.     Result    := FBarcode.Rotation;
  137. End;
  138. Procedure TDBDuckBarCode.SetAddOn (Const Value: String);
  139. Begin
  140.     FBarcode.AddOn    := Value;
  141.   Picture.Graphic    := FBarcode.GetBitmap;
  142.     Invalidate;
  143. End;
  144. Function TDBDuckBarCode.GetAddOn: String;
  145. Begin
  146.     Result    := FBarcode.AddOn;
  147. End;
  148. Procedure TDBDuckBarCode.SetHeightMM (Const Value: Double);
  149. Begin
  150.     FBarcode.HeightMM    := Value;
  151.   Picture.Graphic    := FBarcode.GetBitmap;
  152.     Invalidate;
  153. End;
  154. Function TDBDuckBarCode.GetHeightMM: Double;
  155. Begin
  156.     Result    := FBarcode.HeightMM;
  157. End;
  158. Procedure TDBDuckBarCode.SetColor (Const Value: TColor);
  159. Begin
  160.     FBarcode.ColorBar    := Value;
  161.   Picture.Graphic    := FBarcode.GetBitmap;
  162.     Invalidate;
  163. End;
  164. Function TDBDuckBarCode.GetColor: TColor;
  165. Begin
  166.     Result    := FBarcode.ColorBar;
  167. End;
  168. Procedure TDBDuckBarCode.SetColorBK (Const Value: TColor);
  169. Begin
  170.     FBarcode.ColorBK    := Value;
  171.   Picture.Graphic    := FBarcode.GetBitmap;
  172.     Invalidate;
  173. End;
  174. Function TDBDuckBarCode.GetColorBK: TColor;
  175. Begin
  176.     Result    := FBarcode.ColorBK;
  177. End;
  178. Procedure TDBDuckBarCode.SetFont (Const Value: TFont);
  179. Begin
  180.     FBarcode.Font    := Value;
  181.   Picture.Graphic    := FBarcode.GetBitmap;
  182.     Invalidate;
  183. End;
  184. Function TDBDuckBarCode.GetFont: TFont;
  185. Begin
  186.     Result    := FBarcode.Font;
  187. End;
  188. Procedure TDBDuckBarCode.SetDensity (Const Value: Integer);
  189. Begin
  190.     FBarcode.Density    := Value;
  191.   Picture.Graphic    := FBarcode.GetBitmap;
  192.     Invalidate;
  193. End;
  194. Function TDBDuckBarCode.GetDensity: Integer;
  195. Begin
  196.     Result    := FBarcode.Density;
  197. End;
  198. Procedure TDBDuckBarCode.SetAlignment (Const Value: TAlignment);
  199. Begin
  200.     FBarcode.Alignment    := Value;
  201.   Picture.Graphic    := FBarcode.GetBitmap;
  202.     Invalidate;
  203. End;
  204. Function TDBDuckBarCode.GetAlignment: TAlignment;
  205. Begin
  206.     Result    := FBarcode.Alignment;
  207. End;
  208. Procedure TDBDuckBarCode.SetBarWidth (Const Value: Double);
  209. Begin
  210.     FBarcode.BarWidthMM    := Value;
  211.   Picture.Graphic    := FBarcode.GetBitmap;
  212.     Invalidate;
  213. End;
  214. Function TDBDuckBarCode.GetBarWidth: Double;
  215. Begin
  216.     Result    := FBarcode.BarWidthMM;
  217. End;
  218. Procedure TDBDuckBarCode.SetOptions (Const Value: TBarcodeOptions);
  219. Begin
  220.     FBarcode.Options    := Value;
  221.   Picture.Graphic    := FBarcode.GetBitmap;
  222.     Invalidate;
  223. End;
  224. Function TDBDuckBarCode.GetOptions: TBarcodeOptions;
  225. Begin
  226.     Result    := FBarcode.Options;
  227. End;
  228. Function TDBDuckBarCode.GetDataField: String;
  229. Begin
  230.     Result    := FDataLink.FieldName;
  231. End;
  232. Procedure TDBDuckBarCode.SetDataField (Const Value: String);
  233. Begin
  234.     FDataLink.FieldName := Value;
  235. End;
  236. Function TDBDuckBarCode.GetDataSource: TDataSource;
  237. Begin
  238.     Result := FDataLink.DataSource;
  239. End;
  240. Procedure TDBDuckBarCode.SetDataSource (Value: TDataSource);
  241. Begin
  242.     FDataLink.DataSource := Value;
  243.   if Value <> nil then Value.FreeNotification(Self);
  244. End;
  245. Function TDBDuckBarCode.GetField: TField;
  246. Begin
  247.     Result := FDataLink.Field;
  248. End;
  249. Procedure TDBDuckBarCode.SetBorderStyle(Value: TBorderStyle);
  250. Begin
  251.     if FBorderStyle <> Value then
  252.     Begin
  253.         FBorderStyle := Value;
  254.         RecreateWnd;
  255.     End;
  256. End;
  257. Procedure TDBDuckBarCode.SetPicture(Value: TPicture);
  258. Begin
  259.   FPicture.Assign(Value);
  260. End;
  261. Procedure TDBDuckBarCode.DataChange(Sender: TObject);
  262. Begin
  263.     Picture.Graphic    := nil;
  264.   if Assigned (FDataLink.Field) Then
  265.   Begin
  266.         FBarcode.Text        := FDataLink.Field.AsString;
  267.      Picture.Graphic    := FBarcode.GetBitmap;
  268.   End;
  269.   Invalidate;
  270. End;
  271. Procedure TDBDuckBarCode.CreateParams(var Params: TCreateParams);
  272. Begin
  273.     inherited CreateParams(Params);
  274.     With Params Do
  275.     Begin
  276.         if FBorderStyle = bsSingle Then
  277.             if NewStyleControls and Ctl3D Then
  278.                 ExStyle := ExStyle or WS_EX_CLIENTEDGE
  279.             Else
  280.                 Style := Style or WS_BORDER;
  281.         WindowClass.style := WindowClass.style and not (CS_HREDRAW or CS_VREDRAW);
  282.     End;
  283. End;
  284.  
  285. Procedure TDBDuckBarCode.Loaded;
  286. Begin
  287.     inherited Loaded;
  288.     if (csDesigning in ComponentState) then DataChange(Self);
  289. End;
  290. Procedure TDBDuckBarCode.Paint;
  291. Var
  292.     Size:            TSize;
  293.     R:                TRect;
  294.     S:                String;
  295.     DrawPict:    TPicture;
  296.     Form:            TCustomForm;
  297.     Pal:            HPalette;
  298. Begin
  299.     With Canvas Do
  300.     Begin
  301.         Brush.Style := bsSolid;
  302.         Brush.Color := Color;
  303.      DrawPict                := TPicture.Create;
  304.      Try
  305.             if csPaintCopy in ControlState then
  306.          Begin
  307.            if Assigned (FDataLink.Field) Then
  308.            Begin
  309.                 FBarcode.Text        := FDataLink.Field.AsString;
  310.                     DrawPict.Graphic    := FBarcode.GetBitmap;
  311.            End;
  312.         End
  313.         Else
  314.             DrawPict.Assign (Picture);
  315.             SetRect(R, 0, 0, DrawPict.Width, DrawPict.Height);
  316.             OffsetRect(R, (ClientWidth - DrawPict.Width) div 2,
  317.             (ClientHeight - DrawPict.Height) div 2);
  318.             StretchDraw(R, DrawPict.Graphic);
  319.             ExcludeClipRect(Handle, R.Left, R.Top, R.Right, R.Bottom);
  320.             FillRect(ClientRect);
  321.             SelectClipRgn(Handle, 0);
  322.      Finally
  323.          DrawPict.Free;
  324.      End;
  325.      Form := GetParentForm(Self);
  326.     if (Form <> nil) and (Form.ActiveControl = Self) and
  327.       not (csDesigning in ComponentState) and
  328.       not (csPaintCopy in ControlState) then
  329.     begin
  330.       Brush.Color := clWindowFrame;
  331.       FrameRect(ClientRect);
  332.     end;
  333.   End;
  334. End;
  335. Function TDBDuckBarCode.Print (rLeft, rTop: Real): TRect;
  336. Begin
  337.     Result    := FBarcode.Print (rLeft, rTop);
  338. End;
  339. Function TDBDuckBarCode.PaintPoint (Canvas: TCanvas; pt: TPoint): TRect;
  340. Begin
  341.     Result    := FBarcode.PaintPoint (Canvas, pt);
  342. End;
  343. Procedure TDBDuckBarCode.PaintRect (Canvas: TCanvas; Rc: TRect);
  344. Begin
  345.     FBarcode.PaintRect (Canvas, Rc);
  346. End;
  347. End.
  348.